package com.qf.gh.service.impl;

import com.qf.gh.dao.AddressDao;
import com.qf.gh.dao.OrderDao;
import com.qf.gh.dao.impl.OrderDaoImpl;
import com.qf.gh.domain.Address;
import com.qf.gh.dao.impl.AddressDaoImpl;
import com.qf.gh.service.AddressService;
import com.qf.gh.utils.TxUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class AddressServiceImpl implements AddressService {
    OrderDao orderDao=new OrderDaoImpl();
    AddressDao addressDao=new AddressDaoImpl();
    @Override
    public List<Address> findByUid(int id) {
        return addressDao.findByUid(id);
    }

    @Override
    public void addAddress(Address address) {
        addressDao.addAddress(address);
    }

    @Override
    public void update(Address address) {
        addressDao.update(address);
    }

    @Override
    public void deleteById(int parseInt) {
        long count = orderDao.findCountByAid(parseInt);
        if(count == 0) {
            addressDao.deleteById(parseInt);
        } else {
            System.out.println("与订单关联，不能删除");
        }
    }

    @Override
    public void setDefaultAddress(int parseInt, int id) {
        Connection conn = TxUtils.getConn();
        try {
            conn.setAutoCommit(false);
            addressDao.clearDefaultAddress(id);

//               String s=null;
//               if(!s.equalsIgnoreCase("sfasfaf")){
//                   return;
//               }
            addressDao.setDefaultAddress(parseInt);
            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        } finally {
            TxUtils.remove();
            try {
                conn.setAutoCommit(true);
                conn.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
}
